package com.study.consumer.filter;


import com.study.consumer.Constant;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @Auther: luobiao
 * @Date: 2021/4/27 06:27
 * @Description: 通过servlet的过滤器取出请求中的tag放入到RpcContext的Attachment中
 */
@WebFilter(urlPatterns = "/*")
public class TagFilter implements Filter  {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("hello");
        String tag = servletRequest.getParameter(Constant.DUBBO_TAG);
        if (StringUtils.hasLength(tag)) {
            RpcContext.getContext().setAttachment(Constant.DUBBO_TAG, tag);
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
